// import { User } from 'src/user/user.entity';
// import { Logs } from 'src/logs/logs.entity';
// import { Roles } from "@/decorators/roles.decorator";
import dayjs from "dayjs";
import os from "os";
// import { Menus } from 'src/menus/menu.entity';

// import utc from "dayjs/plugin/utc";
// import timezone from "dayjs/plugin/timezone";
//
// dayjs.extend(utc);
// dayjs.extend(timezone);
export const getEntities = (path: string) => {
    // /users ->User , /logs -> Logs, /roles -> Roles, /menus -> Menus, /auth -> 'Auth'
    const map = {
        // '/users': User,
        // '/logs': Logs,
        // '/roles': Roles,
        // '/menus': Menus,
        // '/auth': 'Auth',
    };
    
    for (let i = 0; i < Object.keys(map).length; i++) {
        const key = Object.keys(map)[i];
        if (path.startsWith(key)) {
            return map[key];
        }
    }
};

export function formatBytes(bytes, decimals = 2) {
    if (+bytes === 0) return "0 Bytes";
    const k = 1024;
    const dm = decimals < 0 ? 0 : decimals;
    const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
    const i = Math.floor(Math.log(+bytes) / Math.log(k));
    return parseFloat((+bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}

//生成N位随机数
export function generateRandomNumber(n = 15) {
    let numbers = "123456789"; // 修改为不包含 0 的数字字符
    let result = "";
    // 第一位数字特殊处理，从 1-9 中随机选取一个数字
    result += numbers.charAt(Math.floor(Math.random() * (numbers.length - 1)) + 1);
    // 生成剩下的 n-1 位数字
    for (let i = 0; i < n - 1; i++) {
        result += numbers.charAt(Math.floor(Math.random() * numbers.length));
    }
    return result;
}

export const transformer = {
    to: (value: Date) => (value),
    from: (value: Date) => (`${dayjs(value).format("YYYY-MM-DD HH:mm:ss")}`)
};


export const customTransformer = {
    to: (value: Date) => (new Date(value)),
    from: (value: Date) => (value ? `${dayjs(value).format("YYYY-MM-DD HH:mm:ss")}` : value)
};

export function argumentsToString(request) {
    let str = ``;
    if (Object.keys(request.params).length > 0) str += `params:${JSON.stringify(request.params)} `;
    if (Object.keys(request.query).length > 0) str += `query:${JSON.stringify(request.query)} `;
    if (Object.keys(request.body).length > 0) str += `body:${JSON.stringify(request.body)} `;
    return str;
}

export const generateMenuTree = (data, parentId) => {
    return data.reduce((tree, menu) => {
        if (menu.parentId === parentId) {
            const children = generateMenuTree(data, menu.id);
            if (children.length > 0) {
                menu.children = children;
            }
            tree.push(menu);
        }
        return tree;
    }, []);
};

//获取本机ip
export function getLocalIP(): string {
    const interfaces = os.networkInterfaces();
    for (let devName in interfaces) {
        const iface = interfaces[devName];
        for (let i = 0; i < iface.length; i++) {
            const alias = iface[i];
            if (alias.family === "IPv4" && alias.address !== "127.0.0.1" && !alias.internal) {
                return alias.address;
            }
        }
    }
}

function extractNestedProperties(obj, parentKey = "") {
    let res = {};
    for (let key in obj) {
        if (typeof obj[key] === "object" && obj[key] !== null) {
            res = { ...res, ...extractNestedProperties(obj[key], `${parentKey}${key}_`) };
        } else {
            res[`${parentKey}${key}`] = obj[key];
        }
    }
    return res;
}

//扁平化处理数据
export function extractNestedPropertiesFromArray(arr) {
    return arr.map(item => extractNestedProperties(item));
}